package cn.com.cpic.insurance.rulecheck.core;

import java.beans.FeatureDescriptor;
import java.util.Iterator;

import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ELResolver;
import javax.el.PropertyNotFoundException;
import javax.el.PropertyNotWritableException;

/**
 * 
 * 
 * @author 代波
 * @since 2011-8-20
 */
public final class BOMContextELResolver extends ELResolver {
	private final BomContext bomContext;

	public BOMContextELResolver() {
		super();
		this.bomContext = null;
	}

	public BOMContextELResolver(BomContext bomContext) {
		super();
		this.bomContext = bomContext;
	}

	@Override
	public Object getValue(ELContext context, Object base, Object property) throws NullPointerException,
			PropertyNotFoundException, ELException {
		if (context == null)
			throw new NullPointerException();

		if (base == null) {
			if (bomContext != null) {
				if (bomContext.containsBom(property)) {
					context.setPropertyResolved(true);
					return bomContext.getBom(property);
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		} else {
			if (BomContext.class.isInstance(base)) {
				if (((BomContext) base).containsBom(property)) {
					context.setPropertyResolved(true);
					return ((BomContext) base).getBom(property);
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		}

		return null;
	}

	@Override
	public Class<?> getType(ELContext context, Object base, Object property) throws NullPointerException,
			PropertyNotFoundException, ELException {
		if (context == null)
			throw new NullPointerException();

		if (base == null) {
			if (bomContext != null) {
				Object val = bomContext.getBom(property);
				if (val != null) {
					context.setPropertyResolved(true);
					return val.getClass();
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		} else {
			if (BomContext.class.isInstance(base)) {
				Object val = ((BomContext) base).getBom(property);
				if (val != null) {
					context.setPropertyResolved(true);
					return val.getClass();
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		}

		return null;
	}

	@Override
	public void setValue(ELContext context, Object base, Object property, Object value) throws NullPointerException,
			PropertyNotFoundException, PropertyNotWritableException, ELException {
		if (context == null)
			throw new NullPointerException();

		if (base == null) {
			if (bomContext != null) {
				if (bomContext.containsBom(property)) {
					context.setPropertyResolved(true);
					bomContext.addBom(property, value);
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		} else {
			if (BomContext.class.isInstance(base)) {
				if (((BomContext) base).containsBom(property)) {
					context.setPropertyResolved(true);
					((BomContext) base).addBom(property, value);
				} else
					throw new PropertyNotFoundException(property.toString());
			}
		}
	}

	/**
	 *
	 */
	@Override
	public boolean isReadOnly(ELContext context, Object base, Object property) throws NullPointerException,
			PropertyNotFoundException, ELException {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 *
	 */
	@Override
	public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *
	 */
	@Override
	public Class<?> getCommonPropertyType(ELContext context, Object base) {
		// TODO Auto-generated method stub
		return null;
	}

}
